package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> list = permissionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        //查出表里所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpressions();
        //获得所有的控制器
        Map<String, Object> controllerMap = ctx.getBeansWithAnnotation(Controller.class);
        //因为是map 我们需要的是他的value
        Collection<Object> collections = controllerMap.values();
        //遍历控制器获取每一个控制器里的方法
        for (Object collection : collections) {
            //利用反射获取控制器的所有方法(不拿父类的,用Declared)
            //因为我们使用shiro注解的方式(@RequiresPermissions)判断权限,底层会用CGLIB动态代理的方式来代理我们的controller
            //所以我们要拿现在的controller的父类的字节码,才能获得真实的方法和方法上的注解
            Class<?> clazz = collection.getClass().getSuperclass();
            Method[] methods = clazz.getDeclaredMethods();
            //遍历每一个方法再来判断这个方法是否有贴权限注解
            for (Method method : methods) {
                //通过getAnnotation方法获得方法上的GivePermission注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if(annotation != null){
                    //有这个注解,就来设置权限对象并插入
                    //拿到注解里面的name字符串
                    String name1 = annotation.value()[1];
                    //获取权限表达式
                    String expression = annotation.value()[0];
                    //防止重复添加,如果该方法权限已经在表里面了就不需要再插入进去
                    if(!expressions.contains(expression)){
                        Permission permission = new Permission();
                        permission.setName(name1);
                        permission.setExpression(expression);
                        //插入到表中
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }


}
